package com.jiao.controller;

import com.jiao.bean.TTrade;
import com.jiao.bean.TTradeQuery;
import com.jiao.bean.vo.JsonObjVo;
import com.jiao.bean.vo.SynthesizeVo;
import com.jiao.bean.vo.TTradeVo;
import com.jiao.service.TTradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 基金查询
 */
@RestController
@RequestMapping("fund")
public class TTradeController {
    @Autowired
    TTradeService tTradeService;


    @GetMapping("/find")
    public JsonObjVo find(@RequestParam Integer year,Integer month) {
        TTradeVo lastMonth = getTotalTTrade(year,month-1);
        TTradeVo currentMonth = getTotalTTrade(year,month);
        TTradeVo yearMonth = getTotalTTrade(year,0);
        SynthesizeVo synthesizeVo = new SynthesizeVo();
        synthesizeVo.setLastMouth(lastMonth);
        synthesizeVo.setCurrentMouth(currentMonth);
        synthesizeVo.setCumulativeYear(yearMonth);
        return JsonObjVo.successReponse(synthesizeVo);
    }

    @GetMapping("/remove")
    public JsonObjVo remove(@RequestParam Integer id) {
        TTrade tTrade = tTradeService.queryById(id);
        if (null == tTrade){
            return JsonObjVo.successReponseMsg("没有找到对应的数据，请确认信息是否有误！");
        }
        tTrade.setState(0);
        int flag = tTradeService.update(tTrade);
        if (flag > 0){
            return JsonObjVo.successReponse("修改成功！");
        }
        return JsonObjVo.failReponse("修改失败！");
    }

    @PostMapping("/add")
    public JsonObjVo add(@RequestBody @Valid TTradeVo tTradeRequestVo) {
        TTrade tTrade = new TTrade();
        tTrade.setCreatedate(String.valueOf(tTradeRequestVo.getYear()));
        tTrade.setMonth(tTradeRequestVo.getMonth());
        tTrade.setOutindividu(tTradeRequestVo.getPersonalOut());
        tTrade.setOutgove(tTradeRequestVo.getOverallOut());
        tTrade.setIncome(tTradeRequestVo.getTotalIncome());
        tTrade.setResiduegrove(tTradeRequestVo.getOverallBalance());
        tTrade.setResidueindividu(tTradeRequestVo.getPersonalBalance());
        tTrade.setAccumulatedgov(tTradeRequestVo.getAccumulatedOverall());
        tTrade.setAccumulatedindividu(tTradeRequestVo.getAccumulatedPersonal());
        int flag = tTradeService.insert(tTrade);
        if (flag > 0){
            return JsonObjVo.successReponse("执行成功！");
        }
        return JsonObjVo.failReponse("网络异常，请稍后重试");
    }

    @PostMapping("/modify")
    public JsonObjVo modify(@RequestBody TTradeVo tTradeReqeustVo) {
        TTrade tTrade = tTradeService.queryById(tTradeReqeustVo.getId());
        if (null == tTrade){
            return JsonObjVo.successReponseMsg("没有找到对应的数据，请确认信息是否有误！");
        }
        tTrade.setOutgove(tTradeReqeustVo.getOverallOut());
        tTrade.setOutindividu(tTradeReqeustVo.getPersonalOut());
        int flag = tTradeService.update(tTrade);
        if (flag > 0){
            return JsonObjVo.successReponse("修改成功！");
        }
        return JsonObjVo.failReponse("网络异常，请稍后重试");
    }


    /**
     * 基金信息按年按月统计
     * @param year
     * @param month
     * @return
     */
    public TTradeVo getTotalTTrade(int year, int month) {
        //基金部分查询
        TTradeQuery tTradeQuery = new TTradeQuery();
        if (month > 0) {
            tTradeQuery.createCriteria()
                    .andCreatedateEqualTo(String.valueOf(year))
                    .andMonthEqualTo(month)
                    .andStateEqualTo(1);
        } else {
            tTradeQuery.createCriteria()
                    .andCreatedateEqualTo(String.valueOf(year))
                    .andStateEqualTo(1);

        }
        List<TTrade> tTradeList = tTradeService.query(tTradeQuery);
        if (tTradeList.size() > 0) {
            int income = 0;
            int accumulatedgov = 0;
            int accumulatedindividu = 0;
            int residuegrove = 0;
            int outgove = 0;
            int residueindividu = 0;
            int outindividu = 0;

            for (TTrade tTrade : tTradeList) {
                income += tTrade.getIncome();
                accumulatedgov += tTrade.getAccumulatedgov();
                accumulatedindividu += tTrade.getAccumulatedindividu();
                residuegrove += tTrade.getResiduegrove();
                outgove += tTrade.getOutgove();
                residueindividu += tTrade.getResidueindividu();
                outindividu += tTrade.getOutindividu();
            }
            //基金信息统计
            TTradeVo tradeVo = new TTradeVo();
            tradeVo.setTotalIncome(income);
            //累计个人和统筹
            tradeVo.setAccumulatedOverall(accumulatedgov);
            tradeVo.setAccumulatedPersonal(accumulatedindividu);
            tradeVo.setYear(year);
            if (month > 1) {
                tradeVo.setId(tTradeList.get(0).getId());
                tradeVo.setMonth(month);
            }
            tradeVo.setOverallBalance(residuegrove);
            tradeVo.setOverallOut(outgove);
            tradeVo.setPersonalBalance(residueindividu);
            tradeVo.setPersonalOut(outindividu);
            return tradeVo;
        }
        return null;
    }
}

